package com.think.in.spring.ioc.overview.container;

import com.think.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import java.util.Map;

/**
 *
 * BeanFactory 作为IOC 容器 , 是Spring 最底层的一个IOC容器，
 *      只是提供一些常用的简单的功能
 * @author : zjj
 * @date : 2021/12/28 12:53
 */
public class BeanFactoryAsIoCContainerDemo {
    public static void main(String[] args) {
        DefaultListableBeanFactory listableBeanFactory = new DefaultListableBeanFactory();

        XmlBeanDefinitionReader xmlBeanDefinitionReader =new XmlBeanDefinitionReader(listableBeanFactory);

        String location = "META-INF/dependency-lookup-context.xml";

        int beanDefinitionCount = xmlBeanDefinitionReader.loadBeanDefinitions(location);
        System.out.println(beanDefinitionCount);

        lookupCollectionByType(listableBeanFactory);
    }

    /**
     * 通过类型查找 集合
     * @param beanFactory
     */
    private static void lookupCollectionByType(BeanFactory beanFactory) {
        if(beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> beansOfType = listableBeanFactory.getBeansOfType(User.class);

            System.out.println("类型查找 ，集合：" + beansOfType);

        }
    }
}
